package com.bookcity.front.webfront.config;

import com.alibaba.fastjson2.JSON;
import com.bookcity.front.webfront.domain.Member;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author oldliu
 * @since 1.0
 */
@Component
public class MyStore {
    private static int time=18000*1000;//存活时间，毫秒
    private static Map<String,D> store=new HashMap<>();
    public MyStore(){
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<");
    }
    static{
        Member m=new Member();
        m.setId(2L);
        m.setDouyinid("_0004MN6xpTUUA0ARR8O7KuMrxxOk2m8EnI6");
        m.setDouyinacc("小书虫168394504541900000");
        m.setAccountname("小书虫168394504541900000");
        D d=new D();

        store.put("1111",new D(m,System.currentTimeMillis(),System.currentTimeMillis()+time));
        /**
         * Member@583b93fc[
         *   id=2
         *   douyinid=_0004MN6xpTUUA0ARR8O7KuMrxxOk2m8EnI6
         *   douyinacc=小书虫168394504541900000
         *   accountname=小书虫168394504541900000
         */
        //m.setDouyinacc();
        //store.put("1",new D(new Member(),time));
    }
    public Object get(String key){

        D o= store.get(key);
        System.out.println(key+">>>>>>>>>>>>>>>>>>>>>"+o);
        System.out.println("store:"+store);
        if(o==null)
            return null;
        if(o.expire<System.currentTimeMillis())
        {
            store.remove(key);
            return null;
        }
        return o.obj;
    }
    public Object remove(String key){
        return store.remove(key);
    }
    public void set(String key, Object o){
        System.out.println(key+",,,,,,"+o+".,,,被放入了token");
        store.put(key,new D(o,System.currentTimeMillis(),System.currentTimeMillis()+time));
        //store.put(key,new D(JSON.toJSON(o),System.currentTimeMillis(),System.currentTimeMillis()+time));
        System.out.println("now..............................."+store);
    }
    public void expireTime(String key){
        if(!store.containsKey(key))
            return;

        store.get(key).setExpire(System.currentTimeMillis()+time);
    }
    public boolean hasKey(String key){
        D o= store.get(key);

        if(o==null||o.expire >=System.currentTimeMillis())
        {
            store.remove(key);
            return false;
        }
        return o.obj!=null;
        //return store.containsKey(key);
    }

    static class   D implements Serializable {
        private Object obj;
        private long start;
        private long expire;//失效时间

        public D(){

        }
        public D(Object obj, long start, long expire) {
            this.obj = obj;
            this.start = start;
            this.expire = expire;
        }
        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }

        public long getStart() {
            return start;
        }

        public void setStart(long start) {
            this.start = start;
        }

        public long getExpire() {
            return expire;
        }

        public void setExpire(long expire) {
            this.expire = expire;
        }



        @Override
        public String toString() {
            return "D{" +
                    "obj=" + obj +
                    ", start=" + start +
                    ", expire=" + expire +
                    '}';
        }
    }
}
